import 'package:flutter/material.dart';
// import 'package:provider/provider.dart';
import '/res/constant.dart';
import '/routers/not_found_page.dart';
import '/routers/routers.dart';
import '/util/handle_error_utils.dart';
import '/util/log_utils.dart';
import 'package:oktoast/oktoast.dart';
import 'package:sp_util/sp_util.dart';


Future<void> main() async {
//  debugProfileBuildsEnabled = true;
//  debugPaintLayerBordersEnabled = true;
//  debugProfilePaintsEnabled = true;
//  debugRepaintRainbowEnabled = true;
  if (Constant.inProduction) {
    /// Release环境时不打印debugPrint内容
    debugPrint = (String? message, {int? wrapWidth}) {};
  }

  /// 异常处理
  handleError(() async {
    /// 确保初始化完成
    WidgetsFlutterBinding.ensureInitialized();

    /// sp初始化
    await SpUtil.getInstance();

    /// 1.22 预览功能: 在输入频率与显示刷新率不匹配情况下提供平滑的滚动效果
    // GestureBinding.instance?.resamplingEnabled = true;
    runApp(MyApp());
  });

  /// 隐藏状态栏，导航栏。为启动页、引导页设置全屏显示。完成后还原。
  // SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
  // 相关问题跟踪：https://github.com/flutter/flutter/issues/73351
}

class MyApp extends StatelessWidget {
  MyApp({super.key, this.home, this.theme}) {
    Log.init();
    Routes.initRoutes();
  }

  final Widget? home;
  final ThemeData? theme;
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    final Widget app = _buildMaterialApp();

    // final Widget app = MultiProvider(
    // providers: [
    // ChangeNotifierProvider(create: (_) => ThemeProvider()),
    // ],
    // child: Consumer<ThemeProvider>(
    //   builder: (_, ThemeProvider provider,) {
    //     return _buildMaterialApp(provider,);
    //   },
    // ),
    // );

    /// Toast 配置
    return OKToast(
        backgroundColor: Colors.black54,
        textPadding:
            const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
        radius: 20.0,
        position: ToastPosition.bottom,
        child: app);
  }

  Widget _buildMaterialApp() {
    // Widget _buildMaterialApp(ThemeProvider provider) {
    return MaterialApp(
      title: 'Young Bill',
      // showPerformanceOverlay: true, //显示性能标签
      debugShowCheckedModeBanner: false, // 去除右上角debug的标签
      // checkerboardRasterCacheImages: true,
      // showSemanticsDebugger: true, // 显示语义视图
      // checkerboardOffscreenLayers: true, // 检查离屏渲染

      // theme: theme ?? provider.getTheme(),
      // darkTheme: provider.getTheme(isDarkMode: true),
      // themeMode: provider.getThemeMode(),
      // home: home ?? const SplashPage(),
      onGenerateRoute: Routes.router.generator,
      navigatorKey: navigatorKey,
      builder: (BuildContext context, Widget? child) {
        /// 保证文字大小不受手机系统设置影响 https://www.kikt.top/posts/flutter/layout/dynamic-text/
        return MediaQuery(
          data:
              MediaQuery.of(context).copyWith(textScaler: TextScaler.noScaling),
          child: child!,
        );
      },

      /// 因为使用了fluro，这里设置主要针对Web
      onUnknownRoute: (_) {
        return MaterialPageRoute<void>(
          builder: (BuildContext context) => const NotFoundPage(),
        );
      },
      restorationScopeId: 'app',
    );
  }
}
